import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt

dir = r"D:\data_cmp\airfoil_csv"


def rotate(ordinate, basic, angle=None):
    '''
        旋转坐标集合
    :param ordinate: 坐标集合
           basic: 旋转基准;tan值为正表示逆时针旋转；为负表示顺时针旋转
    :return: rotate_ordinate
    '''

    # cal angle
    if angle is None:  # 默认旋转到y=0
        angle = -np.arctan(basic[1] / basic[0])
    else:
        angle = -(np.arctan(basic[1] / basic[0]) - angle)

    # 角度为正为逆时针旋转/角度为负为顺时针旋转
    rotate_ordinate = np.zeros(ordinate.shape)
    rotate_ordinate[:, 0] = ordinate[:, 0] * np.cos(angle) - ordinate[:, 1] * np.sin(angle)
    rotate_ordinate[:, 1] = ordinate[:, 0] * np.sin(angle) + ordinate[:, 1] * np.cos(angle)

    # 可视化
    # plt.scatter(rotate_ordinate[:, 0], rotate_ordinate[:, 1])
    # plt.show()

    return rotate_ordinate


def rotate1(ordinate, angle, point):
    '''
        绕某个点旋转坐标集合
    :param ordinate: 坐标集合
           angle: 旋转角度
           point: 绕哪一点进行旋转
    :return: rotate_ordinate
    '''

    # 角度为正为逆时针旋转/角度为负为顺时针旋转
    rotate_ordinate = np.zeros(ordinate.shape)
    rotate_ordinate[:, 0] = (ordinate[:, 0] - point[0]) * np.cos(angle) - (ordinate[:, 1] - point[1]) * np.sin(angle) + \
                            point[0]
    rotate_ordinate[:, 1] = (ordinate[:, 0] - point[0]) * np.sin(angle) + (ordinate[:, 1] - point[1]) * np.cos(angle) + \
                            point[1]

    # 可视化
    # plt.scatter(rotate_ordinate[:, 0], rotate_ordinate[:, 1])
    # plt.show()

    return rotate_ordinate


def move_x(ordinate, move_dis):
    '''
            平移坐标集合(水平)
        :param ordinate: 坐标集合
               move_dis: 移动方向，正表示向左移，负表示向右移
        :return: move_ordinate
    '''

    ordinate[:, 0] += move_dis
    return ordinate


def move_y(ordinate, move_dis):
    '''
            平移坐标集合(垂直)
        :param ordinate: 坐标集合
               move_dis: 移动方向，正表示向上移，负表示向下移
        :return: move_ordinate
    '''

    ordinate[:, 1] += move_dis
    return ordinate


def transform(ordinate):
    '''
        转为逆时针顺序排列的坐标集合
    :param ordinate: 坐标集合;pandas.DataFrame类型
    :return:
    '''

    stack = []
    x = ordinate[:, 0]
    mid = np.argmax(x)
    for i in range(mid, -1, -1):
        stack.append(x[i])
    for i in range(0, mid + 1):
        x[i] = stack[i]
    ordinate[:, 0] = x

    return ordinate


def set0(ordinates):
    '''
        将坐标极其接近0的值设置为0
    :param ordinates: 坐标集合
    :return: 更新后的坐标
    '''
    ordinates = np.asarray(ordinates)
    len = ordinates.shape[0]

    for i in [0, int(len/2), len-1]:
        if abs(ordinates[i, 0]) < 1e-5:
            ordinates[i, 0] = 0
        if abs(ordinates[i, 1]) < 1e-5:
            ordinates[i, 1] = 0
        if abs(ordinates[i, 0] - 1) < 1e-5:
            ordinates[i, 0] = 1

    return ordinates


def func1(ordinates):
    '''

    :return:
    '''

    # examples
    # airfoil leading
    ordinates = np.asarray([[1, 0], [0.6, 0.4], [0, 0], [0.3, -0.2], [1, 0]])  # standard
    ordinates = np.asarray([[1, 0], [0.6, 0.4], [1e-5, 0], [0.1, -0.1], [0.3, -0.2], [1, 0]])
    ordinates = np.asarray([[1, 0], [0.6, 0.4], [0.003, 0.02], [0.005, 0.01], [0.3, -0.2], [1, 0]])

    # ordinates = np.asarray([[1, 0], [0.6, 0.4], [0.003, 0.02], [-0.003, 0.02], [0.3, -0.2], [1, 0]])

    def fmin(x):
        return np.argwhere(x == np.min(x)).flatten()

    def fmax(x):
        return np.argwhere(x == np.max(x)).flatten()

    # airfoil leading

    # airfoil trailing


# test
# ordinates = np.asarray([[1, 1], [2, 2]])
# angle = np.pi / 2
# ordinates = rotate1(ordinates, angle, ordinates[1])
# print(ordinates)
